(function(){
	yanhx.addFunc("field_def",function(){
		yanhx.exec("fieldHandle.open",{
			title:this.attr("title"),
			target:this,
			callBack:function(ret){
				yanhx.exec("fieldHandle.callBack",ret);
			}
		});
	});
	yanhx.addComponent("fieldHandle",{
		template:$("<div class='tagDiv'><div class='content'>" +
				 "<span class='inputSpan'><input id='key'/><span>:</span><input id='desc' /></span></div>" +
				 "<div class='tool'>" +
				 "<button class='btn addField' type='button' callFunc='click:field_add'>添加</button>"+
				 "<button class='btn del' type='button' callFunc='click:field_del'>删除</button>" +
				 "<button class='btn confirm' type='button'  callFunc='click:field_return'>确定</button>" +
				 "<button class='btn cancel' type='button' callFunc='click:field_cancel'>取消</button></div></div>"),
		
		itemTemplate:"<span class='item' callFunc='click:field_check'></span>",
		opt:null,
		uqIdKey:parseInt(Math.random()*10000),
		prefix:"field_",
		isManager:false,
		hasInit:false,
		init:function(){
			this.content=this.template.children("div.content");
			this.inputSpan=this.content.find(".inputSpan");
			this.inputKey=this.inputSpan.find("#key");
			this.inputDesc=this.inputSpan.find("#desc");
			this.tool=this.template.children("div.tool");
			this.addCtrl=this.tool.find(".addField");
			this.delCtrl=this.tool.find(".del");
			this.confirm=this.tool.find(".confirm");
			this.cancel=this.tool.find(".cancel");
			yanhx.addFunc("field_add",this.doAddItem);
			yanhx.addFunc("field_cancel",this.doClose);
			
			yanhx.addFunc("field_del",function(){
				yanhx.exec("fieldHandle.bactchDel");
			});
			yanhx.addFunc("field_check",function(){
				yanhx.exec("fieldHandle.checkItem",this);
			});
			yanhx.addFunc("field_return",function(){
				yanhx.exec("fieldHandle.doReturn");
			});
			this.hasInit=true;
		},
		bactchDel:function(){
			var retBean=[];
			var checks=this.content.find("span.check");
			if(!checks.length){
				yanhx.exec("myWin.alert",{
					content:'至少选择一条记录'
				});
				return;
			}
			checks.each(function(){
				retBean.push($(this).attr("id"));
			});
			$.postJSON("/do?action=template.delField",JSON.stringify({fields:retBean}),function(){
				checks.remove();
			});
		},
		//param{title:标题，target:事件源,callBack： 回调函数}
		open:function(param){
			if(!this.hasInit){
				this.init();
			}
			this.opt=param;
			yanhx.exec("myWin.open",{
				elem:this.template,
				model:true,
				title:param.title?param.title:"",
				onclose:this.onClose
			});
			this.queryField();
		},
		queryField:function(){
			var key=this.opt.target.attr("name");
			$.postJSON("/do?action=template.queryField",{key:key},function(ret){
				$(ret).each(function(){
					yanhx.exec("fieldHandle.initItem",this);
				});
				yanhx.exec("fieldHandle.fillOld");
			});
		},
		fillOld:function(){
			var old=this.opt.target.text();
			if(old&&old!="{}"){
				old=old.substring(1,old.length-1);
				old=old.split(",");
				var len=old.length;
				var all=this.content.find(".item");
				for(var i=0;i<len;i++){
					this.setOldItem(old[i],all);
				}
			}
		}, 
		setOldItem:function(val,all){
			all.each(function(){
				var t=$(this);
				if(t.text()==val){
					t.fire("click");
				}
			});
		},
		initItem:function(item){
			var inst=yanhx.find("fieldHandle");
			var obj=$(inst.itemTemplate).insertBefore(inst.inputSpan);
			obj.text("\""+item.key+"\":\""+item.text+"\"");
			obj.attr("id",item.id);
			return obj;
		},
		checkItem:function(elem){
			if(elem.hasClass("check")){
				elem.removeClass("check");
			}
			else{
				elem.addClass("check");
			}
		},
		showInput:function(){
			this.inputSpan.show();
			this.inputKey.get(0).focus();
			this.isManager=true;
			this.delCtrl.hide();
			this.addCtrl.hide();
			this.cancel.text("放弃");
		},
		doAddItem:function(){
			yanhx.exec("fieldHandle.showInput");
		},
		saveField:function(obj){
			var inst=this;
			if(obj.key){
				$.postJSON("/do?action=template.saveField",obj,function(ret){
					inst.initItem(ret).fire("click");
				});
			}
		},
		doReturn:function(){
			if(this.isManager){
				var obj={
					key:this.inputKey.val().trim(),
					text:this.inputDesc.val().trim(),
					field:this.opt.target.attr("name")
				}
				this.saveField(obj);
				this.reset();
			}
			else{
				var retBean=[];
				this.content.find("span.check").each(function(){
					retBean.push($(this).text());
				});
				this.opt.target.text("{"+retBean.join(",")+"}");
				this.doClose();
			}
		},
		onClose:function(){
			yanhx.exec("fieldHandle.clear");
		},
		doClose:function(){
			if(yanhx.find("fieldHandle").isManager){
				yanhx.exec("fieldHandle.reset");
			}
			else{
				yanhx.exec("myWin.closeWin");
				yanhx.exec("fieldHandle.clear");
			}
		},
		reset:function(){
			this.addCtrl.show();
			this.delCtrl.show();
			this.isManager=false;
			this.cancel.text("取消");
			this.inputSpan.hide();
			this.inputKey.val("");
			this.inputDesc.val("");
		},
		clear:function(){
			this.opt=null;
			this.inputSpan.prevAll().remove();
		}
	});
})();
(function(){
	yanhx.addFunc("singleField",function(){
		yanhx.exec("singleField.open",{
			title:this.attr("title"),
			target:this,
			callBack:function(ret){
				yanhx.exec("singleField.callBack",ret);
			}
		});
	});
	yanhx.addComponent("singleField",{
		template:$("<div class='tagDiv'><div class='content'>" +
				 "<span class='inputSpan'><input id='desc' callFunc='enter:singleField.doEnter' /></span></div>" +
				 "<div class='tool'>" +
				 "<button class='btn addField' type='button' callFunc='click:field_add'>添加</button>"+
				 "<button class='btn del' type='button' callFunc='click:field_del'>删除</button>" +
				 "<button class='btn confirm' type='button'  callFunc='click:field_return'>确定</button>" +
				 "<button class='btn cancel' type='button' callFunc='click:field_cancel'>取消</button></div></div>"),
		
		itemTemplate:"<span class='item' callFunc='clickdbl:singleField.dblclick click:field_check'></span>",
		opt:null,
		uqIdKey:parseInt(Math.random()*10000),
		prefix:"field_",
		isManager:false,
		hasInit:false,
		init:function(){
			this.content=this.template.children("div.content");
			this.inputSpan=this.content.find(".inputSpan");
			this.inputKey=this.inputSpan.find("#desc");
			this.tool=this.template.children("div.tool");
			this.addCtrl=this.tool.find(".addField");
			this.delCtrl=this.tool.find(".del");
			this.confirm=this.tool.find(".confirm");
			this.cancel=this.tool.find(".cancel");
			yanhx.addFunc("field_add",this.doAddItem);
			yanhx.addFunc("field_cancel",this.doClose);
			
			yanhx.addFunc("field_del",function(){
				yanhx.exec("singleField.bactchDel");
			});
			yanhx.addFunc("field_check",function(){
				yanhx.exec("singleField.checkItem",this);
			});
			yanhx.addFunc("field_return",function(){
				yanhx.exec("singleField.doReturn");
			});
			this.hasInit=true;
		},
		bactchDel:function(){
			var retBean=[];
			var checks=this.content.find("span.check");
			if(!checks.length){
				yanhx.exec("myWin.alert",{
					content:'至少选择一条记录'
				});
				return;
			}
			checks.each(function(){
				retBean.push($(this).attr("id"));
			});
			var inst=this;
			$.confirm({
				content:'确定删除选中纪录?',
				y:function(){
					$.java({
						action:inst.action+".delField",
						param:{str_array:retBean},
						callback:function(data){
							$.alert(data.mes);
							checks.remove();
							checks=null;
						}
					});
				}
			})
		},
		//param{title:标题，target:事件源,callBack： 回调函数}
		open:function(param){
			if(!this.hasInit){
				this.init();
			}
			var inst=this;
			this.inputKey.bind("blur",function(){
				if(inst.isManager)
					yanhx.exec("singleField.doReturn");
			});
			this.opt=param;
			this.action=param.target.attr("action");
			this.model=param.target.attr("model");
			yanhx.exec("myWin.open",{
				elem:this.template,
				model:true,
				title:param.title?param.title:"",
				onclose:this.onClose
			});
			this.queryField();
		},
		getKey:function(){
			var key=this.opt.target.attr("name");
			if(!key){
				key=this.opt.target.attr("key");
			}
			return key;
		},
		queryField:function(){
			var key=this.getKey();
			$.java({
				action:this.action+".queryField",
				param:{name:key},
				callback:function(ret){
					$(ret).each(function(){
						yanhx.exec("singleField.initItem",this);
					});
					yanhx.exec("singleField.fillOld");
				}
			});
		},
		fillOld:function(){
			var old=this.getValue();
			var handle=this;
			var allItem=this.content.find(".item");
			if(old){
				if(this.model=="single"){
					this.setOldItem(old,allItem);
				}else{
					$(old.split(",")).each(function(){
						handle.setOldItem(this.toString(),allItem);
					});
				}
			}
		}, 
		setOldItem:function(val,all){
			all.each(function(){
				var t=$(this);
				if(t.text()==val){
					t.fire("click");
				}
			});
		},
		initItem:function(item){
			var inst=yanhx.find("singleField");
			var obj=$(inst.itemTemplate).insertBefore(inst.inputSpan);
			obj.text(item.text);
			obj.attr("id",item.id);
			return obj;
		},
		checkItem:function(elem){
			if(elem.hasClass("check")){
				elem.removeClass("check");
			}
			else{
				//单选模式
				if(this.model=="single"){
					this.content.find("span.check").removeClass("check");
				}else{
					//多选模式
				}
				elem.addClass("check");
			}
		},
		showInput:function(){
			this.inputSpan.show();
			this.inputKey.get(0).focus();
			this.isManager=true;
			this.delCtrl.hide();
			this.confirm.hide();
			this.addCtrl.hide();
			this.cancel.text("放弃");
		},
		doAddItem:function(){
			yanhx.exec("singleField.showInput");
		},
		doEnter:function(){
			this.doReturn();
		},
		saveField:function(obj){
			var inst=this;
			$.java({
				action:this.action+".saveField",
				param:obj,
				callback:function(ret){
					ret.text=ret.name;
					inst.initItem(ret).fire("click");
				}
			});
		},
		dblclick:function(elem){
			if(this.opt.callback){
				this.opt.callback(elem.text());
			}
			else{
				this.opt.target.text(elem.text());
			}
			this.doClose();
		},
		doReturn:function(){
			if(this.isManager){
				var obj={
					name:this.inputKey.val().trim(),
					id:this.getKey()
				}
				if(obj.name)
					this.saveField(obj);
				this.reset();
			}
			else{
				var retBean=[];
				this.content.find("span.check").each(function(){
					retBean.push($(this).text());
				});
			 	if(this.opt.callback){
					this.opt.callback(retBean);
				}
				else
					this.setValue(retBean.join(","));
				this.doClose();
			}
		},
		getValue:function(){
			if(this.opt.target.get(0).tagName.toLowerCase()=="input"){
				return this.opt.target.val();
			}
			else{
				return this.opt.target.text();
			}
		},
		setValue:function(val){
			if(this.opt.target.get(0).tagName.toLowerCase()=="input"){
				this.opt.target.val(val);
			}
			else{
				this.opt.target.text(val);
			}
		},
		onClose:function(){
			yanhx.exec("singleField.clear");
		},
		doClose:function(){
			if(yanhx.find("singleField").isManager){
				yanhx.exec("singleField.reset");
			}
			else{
				yanhx.exec("myWin.closeWin");
				yanhx.exec("singleField.clear");
			}
		},
		reset:function(){
			this.addCtrl.show();
			this.delCtrl.show();
			this.isManager=false;
			this.cancel.text("取消");
			this.inputSpan.hide();
			this.inputKey.val("");
			this.confirm.show();
		},
		clear:function(){
			this.opt=null;
			this.inputSpan.prevAll().remove();
		}
	});
})();